package com.applozic.mobicomkit.uiwidgets.conversation.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.text.Spannable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.applozic.mobicomkit.api.conversation.Message;
import com.applozic.mobicomkit.contact.BaseContactService;
import com.applozic.mobicommons.commons.core.utils.Utils;
import com.applozic.mobicommons.emoticon.EmojiconHandler;
import com.applozic.mobicommons.emoticon.EmoticonUtils;
import com.applozic.mobicommons.people.contact.Contact;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.GiveRewardActivity;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;

public class CustomSendMessageView extends BaseRelativeLayout {
    @BindView(R.id.contactImage)
    CircleImageView circleImageView;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.message_text)
    TextView message_text;
    @BindView(R.id.messageLayout)
    RelativeLayout messageLayout;
    private Message message;
    private BaseContactService contactService;

    public CustomSendMessageView(Context context) {
        super(context);
    }

    public CustomSendMessageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSendMessageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.custom_send_message_list_view);

        circleImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                gotoGivePrize(message, contactService.getContactById(message.getContactIds()));
            }
        });


    }

    @Override
    protected void load(Context context) {

    }

    public void setData(Message message, BaseContactService contactService, EmojiconHandler emojiconHandler) {
        this.message = message;
        this.contactService = contactService;
        GlideHelper.loadImage(getContext().getApplicationContext(), contactService.getContactById(message.getContactIds()).getImageURL(), circleImageView, R.mipmap.chat_default);
        Contact messageContact = contactService.getContactById(message.getContactIds());
        String nice_name = null;
        if (UserInfo.checkIsManager(messageContact.getDisplayName())) {
            nice_name = "管理员" + messageContact.getDisplayName();
            name.setText(Utils.setForegroundColorSpan(nice_name, Color.parseColor("#c74402")));
            CharSequence content = Utils.setForegroundColorSpan(message.getMessage(), Color.parseColor("#c74402"));
            Spannable spannable = EmoticonUtils.getSmiledText(mContext, content, emojiconHandler);
            if (spannable.length() <= 300) {
                message_text.setText(spannable);
            }
        } else {
            nice_name = messageContact.getDisplayName();
            name.setText(nice_name);
            Spannable spannable = EmoticonUtils.getSmiledText(mContext, message.getMessage(), emojiconHandler);
            if (spannable.length() <= 300) {
                message_text.setText(spannable);
            }
        }
    }

    private void gotoGivePrize(Message message, Contact contact) {
        if (!UserInfo.getToken().equals("") && contact != null) {
            if (UserInfo.getIsTest() == 1) {
                utils.Utils.showToast("试玩账号不能打赏");
            } else {
                if (!((FragmentActivity) mContext).getIntent().getBooleanExtra("is_single", false)) {//单人聊天不能打赏
                    Intent intent = new Intent(mContext, GiveRewardActivity.class);
                    intent.putExtra("avatar", contact.getImageURL());
                    intent.putExtra("user", message.getContactIds());
                    mContext.startActivity(intent);
                }
            }
        }
    }
}
